﻿using System.Linq;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Web.Base.Security;
using Web.Game.Models;
using Ayatta.Contract.Extension;
using Ayatta.Common.Extensions;
using Ayatta.Contract.Component;

namespace Web.Game.Controllers
{
    public class HomeController : GameBaseController
    {
        public HomeController(ILogService logService, IBaseService baseService, IExtraService extraService, IStoreService storeService, IOrderService orderService, IGameService gameService, IAuctionService auctionService, IPassportService passportService, IPaymentService paymentService, IMemcachedClient memcachedClient)
            : base(logService, baseService, extraService, storeService, orderService, gameService, auctionService, passportService, paymentService, memcachedClient)
        {
        }

        public ActionResult Index()
        {
            var model = new HomeControllerModel.Index();
            var games = GetGames();
            var articles = GameService.GetGameArticles();

            model.Games = games;
            model.Articles = articles;
            return View(model);
        }

        [HttpPost]
        public ActionResult NoviceCard(int param = 0)
        {
            var identity = User.Identity.Identity();
            var result = new SkResult<string>();
            if (!Request.IsAuthenticated)
            {
                result.Message = "请先登录！";
                return Json(result);
            }
            if (param < 1)
            {
                result.Message = "参数错误！";
                return Json(result);
            }
            var serverId = param;
            var games = GetGames();
            var servers = games.GetAllServers();
            var gameId = servers.First(o => o.Id == serverId).GameId;
            var code = GameService.GetGameNoviceCardCode(gameId, serverId, identity.Id, User.Identity.Name);
            if (!code.IsNullOrEmpty())
            {
                result.Status = true;
                result.Data = code;
            }
            else
            {
                result.Message = "获取新手卡失败！";
            }
            return Json(result);
        }

        public ActionResult LoginUrl(string guid, int? gameId, string param)
        {
            if (!gameId.HasValue || gameId.Value < 1)
            {
                return new EmptyResult();
            }
            var url = GetLoginUri(gameId.Value, guid, param);
            return Redirect(url);
        }

        public ActionResult Check(string guid, int? gameId, string param)
        {
            if (!gameId.HasValue || gameId.Value < 1)
            {
                return new EmptyResult();
            }
            var model = CheckUser(gameId.Value, guid, param);
            return Json(model, JsonRequestBehavior.AllowGet);
        }

        public ActionResult UserInfo(string guid, int? gameId, int? serverId, string param)
        {
            if (!gameId.HasValue || gameId.Value < 1 || !serverId.HasValue || serverId.Value < 1)
            {
                return new EmptyResult();
            }
            var model = GetUser(gameId.Value, serverId.Value, guid, param);
            return Json(model, JsonRequestBehavior.AllowGet);
        }

    }
}
